package com.miracle.system.web;

import com.miracle.common.utils.SearchUtils;
import com.miracle.common.utils.SecurityUtils;
import com.miracle.common.utils.StringUtils;
import com.miracle.system.base.domain.ResultMap;
import com.miracle.system.domain.SysUser;
import com.miracle.system.service.SysUserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：miracle.cui
 * @description：TODO
 * @date ：2020/7/28 8:46
 */
@ApiModel(value = "用户管理类")
@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    SysUserService userService;

    @ApiOperation(value = "用户列表")
    @GetMapping("/list")
    public ResultMap list(SysUser user){
        SearchUtils.startPage();
        List<SysUser> userList = userService.selectUserList(user);
        return ResultMap.getPage(userList);
    }

    /* 增 */
    @ApiOperation(value = "新增用户")
    @PostMapping
    public ResultMap add(@Validated @RequestBody SysUser user){

        user.setCreateBy(SecurityUtils.getUserId());
        user.setPassword(SecurityUtils.encryptPassword("123456"));
        user.setSalt(SecurityUtils.getSalt(user.getPassword()));

        Integer rows = userService.insertUser(user);
        return rows > 0 ? ResultMap.success(user) : ResultMap.error();
    }

    /* 删 */
    @ApiOperation(value = "删除用户")
    @DeleteMapping
    public ResultMap delete(@Validated @RequestBody Integer[] userIds){

        Integer rows = userService.deleteUserByUserIds(userIds);
        return rows > 0 ? ResultMap.success() : ResultMap.error();
    }

    /* 改 */
    @ApiOperation(value = "修改用户")
    @PutMapping
    public ResultMap edit(@Validated @RequestBody SysUser user){

        user.setLastUpdateBy(SecurityUtils.getUserId());

        Integer rows = userService.updateUser(user);
        return rows > 0 ? ResultMap.success(user) : ResultMap.error();
    }

    /* 查 */
    @ApiOperation(value = "查询用户")
    @GetMapping(value = {"/", "/{userId}"})
    public ResultMap getInfo(@PathVariable(value = "userId", required = false) Integer userId){

        if (StringUtils.isNotNull(userId)){
            return ResultMap.success(userService.selectUserByUserId(userId));
        } else {
            return ResultMap.error();
        }
    }

    /* 远程校验 */
    @ApiOperation(value = "远程校验用户")
    @GetMapping(value = {"/remoteCheckUser"})
    public ResultMap remoteCheckUser(SysUser user){
        user.setStatus("enabled");
        SysUser checkUser = userService.remoteCheckUser(user);
        return ResultMap.success(checkUser);
    }

    /* 重置密码 */
    @ApiOperation(value = "重置用户密码")
    @PutMapping("/resetPassword")
    public ResultMap resetPassword(@Validated @RequestBody Integer[] userIds){

        Integer rows = userService.resetPassword(userIds);
        return rows > 0 ? ResultMap.success() : ResultMap.error();
    }
}
